package controller;

import implementor.CustomerImp;
import implementor.ModelImp;
import implementor.SellImp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import daoInterface.CustomerDao;
import daoInterface.ModelDao;
import daoInterface.SellDao;
import dto.Customer;
import dto.Sell;

import model.CustomerReportItems;
import model.CustomerReport;

import utilities.ViewUtilites;
import views.crm.CustomerReportView;

public class CustomerReportController implements ActionListener{
	private CustomerReportView view;
	private CustomerReport model;
	
	public CustomerReportController() {
		
	}

	public CustomerReportController(CustomerReportView view,
			CustomerReport model) {
		super();
		this.view = view;
		this.model = model;
		view.actionlisterner(this);
	}
	
	public CustomerReportView getView() {
		return view;
	}

	public void setView(CustomerReportView view) {
		this.view = view;
	}

	public CustomerReport getModel() {
		return model;
	}

	public void setModel(CustomerReport model) {
		this.model = model;
	}
	
	
	public void reactOnApplyFilter() 
	{
		ModelDao models=new ModelImp();
		Date fromdate=ViewUtilites.convert(view.getTextFieldFromDate().getText());
		Date todate=ViewUtilites.convert(view.getTextFieldToDate().getText());
		CustomerReportItems[] data=models.retreiveCustomerReport(fromdate, todate);
		
		DefaultTableModel dm = (DefaultTableModel) view.getTable().getModel();
		for(int i=0;i<data.length;i++)
		{
			Object[] obj ={data[i].getCustomerName(),data[i].getCustomerPhone(),data[i].getPurchaseDate(),data[i].getPurchaseAmt()};
			dm.addRow(obj);
		}
	}
	public void reactOnPrint()
	{
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		String k=e.getActionCommand();
		
		if(k.contains("APPLY FILTER"))
		{
			reactOnApplyFilter();
		}
		
		if(k.contains("Print"))
		{
			reactOnPrint();
		}
		if(k.equals("Cl1"))
		{
			ViewUtilites.makeDateComponent(view.getTextFieldFromDate());
		}
		if(k.equals("Cl"))
		{
			ViewUtilites.makeDateComponent(view.getTextFieldToDate());
		}
		if(k.contains("Send SMS"))
		{
			ViewUtilites.sendSMS();
		}
		if(k.contains("Send Email"))
		{
			ViewUtilites.sendEmail();
		}
	}
}
